package com.baiyang.app.ui.classification;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baiyang.app.databinding.FragmentClassificationBinding;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class ClassificationFragment extends Fragment {

    private ClassificationViewModel classificationViewModel;
    private FragmentClassificationBinding binding;
    private ClassificationViewModel viewModel;
    private ClassificationService service;
    private TabLayoutMediator mediator;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        classificationViewModel =
                new ViewModelProvider(this).get(ClassificationViewModel.class);

        binding = FragmentClassificationBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        viewModel = new ViewModelProvider(this).get(ClassificationViewModel.class);
        service = new ClassificationService(getContext(),viewModel);
        init();
        initHandler();
        return root;
    }

    private void initHandler() {
        viewModel.getGetTypeResult().observe(getViewLifecycleOwner(), new Observer<JSONObject>() {
            private int activeColor = Color.parseColor("#ff678f");
            private int normalColor = Color.parseColor("#666666");
            private int normalSize = 14;
            @Override
            public void onChanged(JSONObject e) {
                String result = e.getString("result");
                if ("SUCCESS".equals(result)) {
                    JSONArray list = e.getJSONArray("list");
                    if (list != null && list.size() > 0) {
                        List<String> tabs = new ArrayList<>();
                        for (int i = 0; i < list.size(); i++) {
                            JSONObject object = list.getJSONObject(i);
                            String tab = object.getString("typeName");
                            tabs.add(tab);
                        }
                        // 设置
                        binding.viewPager.setAdapter(new FragmentStateAdapter(getChildFragmentManager(), getLifecycle()) {
                            @NonNull
                            @Override
                            public Fragment createFragment(int position) {
                                Fragment fragment = (Fragment) new ItemTypeFragment("" + list.getJSONObject(position).getString("id"));
                                return fragment;
                            }

                            @Override
                            public int getItemCount() {
                                return tabs.size();
                            }
                        });
                        mediator = new TabLayoutMediator(binding.pagerTitle, binding.viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
                            @Override
                            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                                //这里可以自定义TabView
                                TextView tabView = new TextView(ClassificationFragment.this.getContext());

                                int[][] states = new int[2][];
                                states[0] = new int[]{android.R.attr.state_selected};
                                states[1] = new int[]{};

                                int[] colors = new int[]{activeColor, normalColor};
                                ColorStateList colorStateList = new ColorStateList(states, colors);
                                tabView.setText(tabs.get(position));
                                tabView.setTextSize(normalSize);
                                tabView.setTextColor(colorStateList);
                                tab.setCustomView(tabView);
                            }
                        });
                        //要执行这一句才是真正将两者绑定起来
                        mediator.attach();
                    }
                } else {
                    String detail = e.getString("detail");
                    Toast.makeText(getContext(), detail, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void init() {
        service.getType();
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}